<template>
  <div>
    <div class="card" style="margin-bottom: 10px">
      <el-input placeholder="请输入医生姓名查询" style="width: 200px" v-model="data.name"></el-input>
      <el-button type="info" plain style="margin-left: 10px" @click="load(1)">查询</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
    </div>

    <div class="card">
      <div style="padding: 15px 20px">
        <el-row :gutter="20">
          <el-col :span="6" v-for="item in data.tableData" style="margin-bottom: 25px">
            <div style="text-align: center;background-color: #cfeff3" class="card">
              <img :src="item.avatar" style="width: 100px;height: 100px;border-radius: 50%">
              <div style="font-weight: 550;margin-top: 15px">
                {{item.name}}<span style="margin-left: 10px;font-weight: 500">{{item.position}}</span>
              </div>
              <div style="margin-top: 20px">
                <el-button type="info" @click="handelAddRegisterForm(item.id)">预约挂号</el-button>
              </div>
            </div>

          </el-col>
        </el-row>
      </div>


      <div class="pagination">
        <el-pagination
            style="margin-top: 5px"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="data.pageNum"
            :page-sizes="[3, 5, 10, 15, 20]"
            :page-size="data.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="data.total">
        </el-pagination>
      </div>

    </div>

    <el-dialog title="挂号单" v-model="data.formVisible" width="50%" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="data.form" label-width="150px" style="padding-right: 50px" :rules="rules" ref="reverseForm">
        <el-form-item label="挂号日期" prop="registrationDate">
          <el-date-picker format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                          v-model="data.form.registrationDate" style="width: 100%" placeholder="挂号日期" disabled></el-date-picker>
        </el-form-item>
        <el-form-item label="预约就诊日期时间" prop="visitDateTime" >
          <el-date-picker type="datetime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
                          :disabled-date="disabledDate"
                          v-model="data.form.visitDateTime" style="width: 100%" placeholder="预约就诊日期"></el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.formVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </span>
      </template>
    </el-dialog>


  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import request from '@/utils/request'
import { ElMessageBox, ElMessage } from 'element-plus'

const data = reactive({
  tableData: [],  // 表格数据
  pageNum: 1,     // 当前页码
  pageSize: 10,   // 每页显示数量
  total: 0,       // 总记录数
  name: null,     // 搜索条件
  formVisible: false, // 表单显示状态
  form: {},       // 表单数据
  ids: [],        // 选中数据的ID
  user: JSON.parse(localStorage.getItem('system-user') || '{}'),
})
const reverseForm = ref()
const load = (pageNum = data.pageNum) => {
  data.pageNum = pageNum
  request.get('/doctor/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      name: data.name,
    }
  }).then(res => {
    if (res.code === '200') {
      console.log(res.data)
      data.tableData = res.data?.list
      data.total = res.data?.total
    } else {
      ElMessage.error(res.msg)
    }
  })
}

// 重置筛选条件
const reset = () => {
  data.name = null
  load()
}

// 处理页码变化
const handleCurrentChange = (pageNum) => {
  load(pageNum)
}
const handleSizeChange=(pageSize)=> {
  data.pageSize = pageSize
  load()
}
load()

const handelAddRegisterForm = (doctorId) => {
  data.form = {}
  data.formVisible = true
// 获取当前日期
  const currentDate = new Date();

  // 格式化为 YYYY-MM-DD 格式
  const year = currentDate.getFullYear();
  const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，因此需要加 1
  const day = String(currentDate.getDate()).padStart(2, '0');

  // 设置 registrationDate 为当前日期
  data.form.registrationDate = `${year}-${month}-${day}`;
  data.form.doctorId = doctorId
}

const disabledDate = (time) => {
  return time.getTime() < Date.now();
}


// 保存数据
const save = () => {
  const url = '/RegistrationForm/add'
  const method = 'POST'
  data.form.patientId = data.user.id
  data.form.status = '等待处理'
  request({
    url,
    method,
    data: data.form,
  }).then(res => {
    if (res.code === '200') {
      load()
      ElMessage.success('预约成功')
      data.formVisible = false
    } else {
      ElMessage.error(res.msg)
    }
  })
}

//表单校验
const submitForm = () => {
  reverseForm.value.validate((valid) => {
    if (valid) {
      save()
    } else {
      ElMessage.error('请填写必须的参数！');
      return false;
    }
  });
}
// 校验规则
const rules = {
  visitDateTime: [
    { required: true, message: '预约就诊日期时间不能为空', trigger: 'change' }
  ],
}
</script>
